package error_checking;

public class PurchaseInputChecker extends ErrorChecker{	
	
	public PurchaseInputChecker(String[] purchaseAmts){
		process(purchaseAmts);
	}
	
	public void process(String[] purchaseAmts){
		super.setErrorFalse();
		if(!isNumStr(purchaseAmts[5])){
			super.setErrorTrue("Invalid number of wagon tongues!");
		}
		if(!isNumStr(purchaseAmts[4])){
			super.setErrorTrue("Invalid number of wagon wheels!");
		}
		if(!isNumStr(purchaseAmts[3])){
			super.setErrorTrue("Invalid number of wagon axles!");
		}
		if(!isNumStr(purchaseAmts[2])){
			super.setErrorTrue("Invalid quantity of clothing!");
		}
		if(!isNumStr(purchaseAmts[1])){
			super.setErrorTrue("Invalid quantity of food!");
		}
		if(!isNumStr(purchaseAmts[0])){
			super.setErrorTrue("Invalid number of oxen!");
		}
	}
	
	/**
	 * Takes in a string and determines whether or not it is made up of numbers
	 * 
	 * @param str
	 *            the string
	 * @return whether or not its made of numbers
	 */
	private boolean isNumStr(String str) {
		boolean isNum = true;
		if (str==null)
			return true;
		if(str.equals(""))
			return true;
		for (int i = 0; i < str.length(); i++) {
			if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
				return false;
			}
		}
		try{
			int n = Integer.parseInt(str);
		}
		catch(NumberFormatException e){ 
			return false; 
		}
		return isNum;
	}

	
}


